# Copyright (C) 2007 "dbsolution sas" 
#
# This file is part of Weebotee.
#
# Weebotee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Weebotee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Weebotee.  If not, see <http://www.gnu.org/licenses/>.

from django.test import TestCase
from apps.core.models.section import Section
from apps.core.models.article import Article
from django.contrib.auth.models import User

class Backend(TestCase):
    fixtures = ['resources/test.json']

    def test_sections(self):
        r = self.client.login(username="carlo", password="test")
        self.assert_(r, "Login failed")

        r = self.client.get("/admin/contents/1/subsection/add/")
        self.failUnlessEqual(r.status_code, 200, "Add section returns %s instead of 200"%r.status_code)
        
        post = {"viewtype":"core:Base", "status":"3", "parent":"", "name":"test_section", "description":"test", "order":"1", "slug":"test", "pagetitle":"test", "keywords":"test", "owner":"1", "owner_alias":"admin"}
        r = self.client.post("/admin/contents/1/subsection/add/", post)
        
        try:
            s = Section.objects.get(name="test_section")
        except:
            s = None

        self.assert_(s, "Section test_section hasn't been created")

        r = self.client.get("/admin/contents/%s/"%s.id)
        self.failUnlessEqual(r.status_code, 200, "Edit section returns %s instead of 200"%r.status_code)

        post["name"] = "test2_section"
        r = self.client.post("/admin/contents/%s/"%s.id, post)

        s = Section.objects.get(id=s.id)

        self.failUnlessEqual(s.name, "test2_section", "Section hasn't been edited")

        r = self.client.get("/admin/contents/%s/delete/"%s.id)
        
        try:
            s = Section.objects.get(id=s.id)
        except:
            s = None
        
        self.assert_(not s, "Section hasn't been deleted")

    def test_articles(self):
        r = self.client.login(username="carlo", password="test")
        self.assert_(r, "Login failed")

        r = self.client.get("/admin/contents/1/articles/add/")
        self.failUnlessEqual(r.status_code, 200, "Add article returns %s instead of 200"%r.status_code)

        post = {"title":"test", "subtitle":"test", "text":"test", "excerpt":"test", "status":"3", "section":"1", "publication_start":"2007-01-01 10:00:00", "publication_end":"2008-01-01 10:00:00", "slug":"test", "keywords":"test", "owner":"1", "order":"0"}
        r = self.client.post("/admin/contents/1/articles/add/", post)

        try:
            a = Article.objects.get(related_element__title="test")
        except:
            a = None

        self.assert_(a, "Article test hasn't been created")

        r = self.client.get("/admin/articles/%s/"%a.related_element.id)

        self.failUnlessEqual(r.status_code, 200, "Edit article returns %s instead of 200"%r.status_code)

        post["title"] = "test2"
        r = self.client.post("/admin/articles/%s/"%a.related_element.id, post)

        a = Article.objects.get(id=a.id)

        self.failUnlessEqual(a.related_element.title, "test2", "Article hasn't been edited")

        r = self.client.get("/admin/articles/%s/delete/"%a.related_element.id)
        
        try:
            a = Article.objects.get(id=a.related_element.id)
        except:
            a = None
        
        self.assert_(not a, "Article hasn't been deleted")

        
